import Vue from 'vue'
import Router from 'vue-router'
import store from '@/vuex/store'

Vue.use(Router)

export default new Router({
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    // 让页面滚回顶部
    return {x:0, y:0}
  },
  routes: [
    {
      path: '/',
      name: 'root',
      redirect: '/home'
    }, {
      path: '/home',
      name: 'home',
      redirect: `/home/${store.state.defaultField}`,
      component: resolve => require(['@/view'], resolve),
      children: [
        {
          path: ':field',
          name: 'homePage',
          component: resolve => require(['@/view/home'], resolve),
          props: true
        }
      ]
    }, {
      path: '/article',
      name: 'article',
      redirect: '/home',
      component: resolve => require(['@/view'], resolve),
      children: [
        {
          path: ':id',
          name: 'detail',
          component: resolve => require(['@/view/articles/detail'], resolve),
          props: true
        }
      ]
    }, {
      path: '/list',
      name: 'list',
      redirect: `/list/${store.state.defaultField}`,
      component: resolve => require(['@/view'], resolve),
      children: [
        {
          path: ':field',
          name: 'listPage',
          component: resolve => require(['@/view/articles/list'], resolve),
          props: true
        }
      ]
    },
    { path: '*', redirect: '/home'}
  ]
})
